package view.clock_panel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JPanel;

import clock.Clock;

import structure.ParticipantVector;
import view.Launch;

import listener.ListChangeEvent;
import listener.ListChangeListener;
import listener.ParticipantChangeEvent;
import listener.ParticipantChangeListener;
import listener.ReachedZeroEvent;
import listener.ReachedZeroListener;
import model.CurrentParticipant;
import model.Debate;

public class ClockPanel extends JPanel implements ParticipantChangeListener{
	private static final long serialVersionUID = -7363310822932105836L;
	
	private CounterButton countBtn;
	private ClockChangeButton pluss, minus;
	private DisplayClock mDisplay, sDisplay;
	
	private JPanel centerPanel;
	
	
	public ClockPanel(Debate debate) {
		super(new FlowLayout());
		
		centerPanel = new JPanel(new GridLayout(1,4));
		
		minus = new DecreaseButton("-");
		minus.setEnabled(false);
		pluss = new IncreaseButton("+");
		pluss.setEnabled(false);
		
		mDisplay = new DisplayClock();
		mDisplay.setEditable(false);
		mDisplay.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {}
			public void focusGained(FocusEvent e) {
				setClockHand(ClockChangeButton.MINUTE);
			}});
		sDisplay = new DisplayClock();
		sDisplay.setEditable(false);
		sDisplay.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {}
			public void focusGained(FocusEvent e) {
				setClockHand(ClockChangeButton.SECOND);
			}});

		centerPanel.add(minus);
		centerPanel.add(mDisplay);
		centerPanel.add(sDisplay);
		centerPanel.add(pluss);
		
		countBtn = new CounterButton(debate.getSecondTimer());
		countBtn.setEnabled(false);
		
		this.add(centerPanel, BorderLayout.CENTER);
		this.add(countBtn, BorderLayout.SOUTH);
		
		addListChangeListeners(debate.getParticipants());
		addParticipantChangeListeners(debate.getCurrentParticipant());
	}
	
	private void setClockHand(int clock){
		try {
			minus.countOn(clock);
			pluss.countOn(clock);
		} catch (NullPointerException arg) {
			if (Launch.DEBUG) arg.printStackTrace();
		}
	}
	
	private void setGUIEnable(boolean b) {
		minus.setEnabled(b);
		pluss.setEnabled(b);
		countBtn.setEnabled(b);
	}
	
	public void addParticipantChangeListeners(CurrentParticipant p) {
		p.addParticipantChangeListener(minus);
		p.addParticipantChangeListener(pluss);
		p.addParticipantChangeListener(countBtn);
		
		p.addParticipantChangeListener(this);
	}
	
	public void addListChangeListeners(ParticipantVector list) {
		list.addListChangeListener(new ListChangeListener() {
			public void listChanged(ListChangeEvent e) {
				validateGUI(e.getSource());
			}});
	}

	private void validateGUI(ParticipantVector e) {
		if (e.isEmpty()) {
			countBtn.setText("Add participant!");
		} else {
			countBtn.setText("Select a participant!");
		} setGUIEnable(false);
		mDisplay.setText("00");
		sDisplay.setText("00");
	}

	public void participantChange(ParticipantChangeEvent e) {
		Clock c = e.getSource().getClock();
		if (!c.timeIsUp()) {
			setGUIEnable(true);
			
			sDisplay.setText(c.getSecond() + "");
			mDisplay.setText(c.getMinute() + "");
			
			c.addReachedZeroListener(countBtn);
			c.getMinute().addTimeChangeListener(mDisplay);
			c.getSecond().addTimeChangeListener(sDisplay);
			
			c.addReachedZeroListener(new ReachedZeroListener() {
				public void reachedZero(ReachedZeroEvent e) {
					setGUIEnable(false);
				}});
		} else {
			setGUIEnable(false);
			sDisplay.setText("00");
			mDisplay.setText("00");
		}
	}
}
